//
//  SCWelcomeViewController.swift
//  weibo
//
//  Created by 阿城 on 15/12/8.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit
import SDWebImage
import SnapKit

class SCWelcomeViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
        
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        icon.snp_updateConstraints { (make) -> Void in
            make.centerY.equalTo(view).offset(-180)
        }
        
        let bgView = UIImageView(image: UIImage.getLaunchImage())
        
        view.addSubview(bgView)
        
        UIView.animateWithDuration(1, animations: { () -> Void in
            bgView.transform = CGAffineTransformMakeScale(2, 2)
            bgView.alpha = 0.1
            }) { (_) -> Void in
                bgView.removeFromSuperview()
        }
      
        //头像动画
        UIView.animateWithDuration(2, delay: 0.9, usingSpringWithDamping: 0.8, initialSpringVelocity: 5, options: [], animations: { () -> Void in
                self.icon.alpha = 1.0
                self.view.layoutIfNeeded()
            }) { (_) -> Void in
        }
        
        //文本动画
        UIView.animateWithDuration(1, delay: 2, options: [], animations: { () -> Void in
            self.welcomeText.alpha = 1
            }) { (_) -> Void in
                //延时操作
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (Int64)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), { () -> Void in
                    //动作之后
                    NSNotificationCenter.defaultCenter().postNotificationName(kSwitchVcNotifcation, object: self)
                })
        }
        
    }
    
    private func setupUI(){
    
        view.addSubview(bgView)
        view.addSubview(icon)
        view.addSubview(welcomeText)
        
        icon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.size.equalTo(CGSize(width: 90, height: 90))
            make.centerY.equalTo(view).offset(-120)
        }
        
        welcomeText.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(icon.snp_bottom).offset(10)
        }
        
        welcomeText.alpha = 0
        icon.alpha = 0
        
    }
    
    private lazy var bgView : UIImageView = {
       
        let bg = UIImageView(image: UIImage(named: "ad_background"))
        return bg
        
    }()
    
    private lazy var icon: UIImageView = {
       
        let iconView = UIImageView(image: UIImage(named: "avatar_default_big"))
        iconView.sd_setImageWithURL(NSURL(string: SCUserInfoViewModel.shareInfoViewModel.userInfoMod!.avatar_large!), placeholderImage: UIImage(named: "avatar_default_big"))
        iconView.cornerRadius = 45
        return iconView
    }()
    
    private lazy var welcomeText: UILabel = {
        
        let lab = UILabel()
        lab.text = "欢迎回来"
        lab.sizeToFit()
        lab.textColor = UIColor.darkGrayColor()
        return lab
        
    }()
    
    
}
